import { Util } from '../../utils/util.js';
import { Definition } from '../common/definition.js';
import { PermissionUnit } from './permissionUnit.js';

export class MenuData {
    constructor(code = '') {
        this.code = code;
        this._name = code;
        this.funCode = '';
        this.excludeShortcut = false;
        this.excludePhoneMenu = false;
        this.excludeMenuTree = false;
        this.subMenuList = [];
        this.permissionList = [];

        // 不在菜单中显示，但是带有路由信息，通过其他路径跳转显示
        this.hideInMenu = false;

        // web 端路由信息
        this.webRouteInfo = {}
    }

    static empty() {
        return new MenuData('');
    }

    static clone(cloneData) {
        const newData = new MenuData(cloneData.code);
        newData._name = cloneData._name;
        newData.funCode = cloneData.funCode;
        newData.path = cloneData.path;
        newData.excludeShortcut = cloneData.excludeShortcut;
        newData.excludePhoneMenu = cloneData.excludePhoneMenu;
        newData.excludeMenuTree = cloneData.excludeMenuTree;
        newData.hideInMenu = cloneData.hideInMenu;

        
        newData.webRouteInfo = JSON.parse(JSON.stringify(cloneData.webRouteInfo));
    
        

        newData.subMenuList = cloneData.subMenuList.map(item => MenuData.clone(item));
        newData.permissionList = cloneData.permissionList.map(item => PermissionUnit.clone(item));

        return newData;
    }

    get name() {
        return this._name; // 你可以自行接入 i18n 实现翻译
    }

    initFunctionMenuPermissionMap(dataMap) {
        this.subMenuList.forEach(item => item.initFunctionMenuPermissionMap(dataMap));
        this.permissionList.forEach(item => {
            dataMap[item.code] = item;
        });
        if (this.funCode) {
            dataMap[this.funCode] = this;
        }
        if (this.code) {
            dataMap[this.code] = this;
        }
    }

    getLeafTreeNode(nodeList) {
        if (this.subMenuList.length > 0) {
            this.subMenuList.forEach(item => item.getLeafTreeNode(nodeList));
        } else if (this.permissionList.length > 0) {
            this.permissionList.forEach(item => nodeList.push(item.code));
        } else if (this.funCode) {
            nodeList.push(this.funCode);
        }
    }

    getCodeNameMap(map) {
        this.subMenuList.forEach(item => item.getCodeNameMap(map));
        this.permissionList.forEach(item => {
            map[item.code] = item.name;
        });
        if (this.funCode) {
            map[this.funCode] = this.name;
        }
    }

    maxMenuItemDataLength() {
        let count = 0;
        this.subMenuList.forEach(item => {
            if (item.subMenuList.length > count) {
                count = item.subMenuList.length;
            }
        });
        return count;
    }

    clearUnselectedNode(checkedNodeCode) {
        let ret = false;

        this.subMenuList = this.subMenuList.filter(item => {
            const keep = item.clearUnselectedNode(checkedNodeCode);
            ret = ret || keep;
            return keep;
        });

        this.permissionList = this.permissionList.filter(item => {
            const keep = checkedNodeCode.includes(item.code);
            ret = ret || keep;
            return keep;
        });

        if (checkedNodeCode.includes(this.funCode)) {
            ret = true;
        }

        return ret;
    }

    static fromJson(json) {
        //console.log("MenuData.fromJson", json);
        const data = new MenuData(Util.jsonToString(json.menuCode));
        data.funCode = Util.jsonToString(json.funCode);
        data._name = Util.jsonToString(json.name);
        data.excludeShortcut = Util.jsonToBool(json.excludeShortcut, false);
        data.excludePhoneMenu = Util.jsonToBool(json.excludePhoneMenu, false);
        data.excludeMenuTree = Util.jsonToBool(json.excludeMenuTree, false);        
        data.hideInMenu = Util.jsonToBool(json.hideInMenu, false);        

        if (Array.isArray(json.menuData)) {
            data.subMenuList = json.menuData.map(item => MenuData.fromJson(item));
        }

        if (Array.isArray(json.permissionData)) {
            data.permissionList = json.permissionData.map(item => PermissionUnit.fromJson(item));
        }        

        if(Definition.isWeb && json.webRouteInfo){
            data.webRouteInfo = JSON.parse(JSON.stringify(json.webRouteInfo));           
        }

       // console.log("MenuData.fromJson", json, data);

        return data;
    }

    toJson() {
        const json = {
            menuCode: this.code,
            funCode: this.funCode,
            name: this._name,
        };

        if (this.excludeShortcut) json.excludeShortcut = this.excludeShortcut;
        if (this.excludePhoneMenu) json.excludePhoneMenu = this.excludePhoneMenu;
        if (this.excludeMenuTree) json.excludeMenuTree = this.excludeMenuTree;

        if (this.subMenuList.length > 0) {
            json.menuData = this.subMenuList.map(item => item.toJson());
        }

        if (this.permissionList.length > 0) {
            json.permissionData = this.permissionList.map(item => item.toJson());
        }

        return json;
    }
}
